import event from "@/kim/utils/event";
import store from "@/kim/store";

let regex_img = /\[img:(?<url>.+?)\]/g;
let regex_file = /file\((?<name>.+?)\)\[(?<url>.+?)\]/g;
let rex_only_img = /^\[img:(?<url>.+?)\]$/g;
let rex_only_file = /^file\((?<name>.+?)\)\[(?<url>.+?)\]$/g;
const cloneRegex = regex => new RegExp(regex.source, regex.flags);

let e = event.e;

console.log('富文本解析器',store.state)

function ImageHandle(text) {
    let match
    while (match=cloneRegex(regex_img).exec(text)) {
        let t1 = text.substring(0,match.index)
        let t2 = text.substring(match.index+match[0].length,text.length)
        let imgTmp = `<img src="${match[1]}" 
                            data-type=""
                            onclick="${e.img_click}(this)" 
                            onload="${e.img_load}(this)" 
                            ondblclick="${e.img_dblclick}(this)">`
        text=t1+imgTmp+t2;
    }
    return text;
}


function FileHandle(text) {
    let match
    while (match=cloneRegex(regex_file).exec(text)) {
        let t1 = text.substring(0,match.index)
        let t2 = text.substring(match.index+match[0].length,text.length)
        let str = `<span class="icon iconfont icon-folder-close"></span>`
        let title = `<span class="filename" title="${match[1]}">${match[1]}</span>`;
        let url = match[2];
        let div2 = `<a href="javascript:void(0)" onclick="${e.file_copy}('${url}',this)" src="${url}" title="${url}">复制链接</a>
                    <a href="${url}" target="_blank">下载</a>`
        let imgTmp = `<div class="file"><div>${str}${title}</div><div>${div2}</div></div>`
        text=t1+imgTmp+t2;
    }
    return text;
}

let str = '你好啊[img:http://localhost:8092/files/4a772ad1-f52e-4394-b8bc-e3344f53a98f.jpg]阿斯顿发送到[img:http://www.baidu.com/3.png]物流双方都'


function checkImage(text) {
    return cloneRegex(regex_img).test(text);
}

function checkFile(text) {
    return cloneRegex(regex_file).test(text);
}

function isOnlyFile(text) {
    return cloneRegex(rex_only_file).test(text.trim());
}

function isOnlyImage(text) {
    return cloneRegex(rex_only_img).test(text.trim());
}

export default {
    ImageHandle,FileHandle,checkImage,checkFile,isOnlyFile,isOnlyImage
}
